package com.ruoyi.buiness.controller;

import com.ruoyi.buiness.domain.Phone;
import com.ruoyi.buiness.service.IPhoneService;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * phoneController
 *
 * @author ruoyi
 * @date 2020-12-19
 */
@Controller
@RequestMapping("/system/phone")
public class PhoneController extends BaseController {
    private String prefix = "system/phone";

    @Autowired
    private IPhoneService phoneService;

    @RequiresPermissions("system:phone:view")
    @GetMapping()
    public String phone() {
        return prefix + "/phone";
    }

    /**
     * 查询phone列表
     */
    @RequiresPermissions("system:phone:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Phone phone) {
        startPage();
        List<Phone> list = phoneService.selectPhoneList(phone);
        return getDataTable(list);
    }

    /**
     * 导出phone列表
     */
    @RequiresPermissions("system:phone:export")
    @Log(title = "phone", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Phone phone) {
        List<Phone> list = phoneService.selectPhoneList(phone);
        ExcelUtil<Phone> util = new ExcelUtil<Phone>(Phone.class);
        return util.exportExcel(list, "phone");
    }

    /**
     * 新增phone
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存phone
     */
    @RequiresPermissions("system:phone:add")
    @Log(title = "phone", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Phone phone) {
        return toAjax(phoneService.insertPhone(phone));
    }

    /**
     * 修改phone
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Phone phone = phoneService.selectPhoneById(id);
        mmap.put("phone", phone);
        return prefix + "/edit";
    }

    /**
     * 修改保存phone
     */
    @RequiresPermissions("system:phone:edit")
    @Log(title = "phone", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Phone phone) {
        return toAjax(phoneService.updatePhone(phone));
    }

    /**
     * 删除phone
     */
    @RequiresPermissions("system:phone:remove")
    @Log(title = "phone", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(phoneService.deletePhoneByIds(ids));
    }

    // 导入方法
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Phone> util = new ExcelUtil<Phone>(Phone.class);
        List<Phone> dataList = util.importExcel(file.getInputStream());
        String message = phoneService.importPhone(dataList);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<Phone> util = new ExcelUtil<Phone>(Phone.class);
        return util.importTemplateExcel("通讯录数据");
    }
}
